//
//  YCLVisitorView.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/9.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

//定义代理协议
protocol YCLVisitorViewDelegate: NSObjectProtocol {

    func visitorViewDidClickRegisterBtn()
    func visitorViewDidClickLoginBtn()
}

class YCLVisitorView: UIView {

    //2.设置代理属性
    weak var delegate:YCLVisitorViewDelegate?
    
    override init(frame: CGRect) {
        //调用父类方法
        super.init(frame: frame)
        
        //设置UI
        setupUI()
        
        //设置背景颜色
        backgroundColor = UIColor.init(white: 237/255.0, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //定义一个供外界调用的方法，设置访客界面的内容
    func setupVisitorInfo(iconViewImgName: String, describeLabelText:String) {
        
        iconView.image = UIImage.init(named: iconViewImgName)
        describeLabel.text = describeLabelText
        
        //隐藏小房子
        homeView.hidden = true
        
        //隐藏遮盖
        shadowView.hidden = true
    }
    
    //定义一个方法，实现旋转动画
    func startRotation() {
        
        //核心动画
        let animation = CABasicAnimation.init(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        
        //动画完成不要移除动画
        animation.removedOnCompletion = false
        //添加核心动画
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    private func setupUI() {
        //1.添加子控件(懒加载)
        addSubview(iconView)
        addSubview(homeView)
        addSubview(describeLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(shadowView)
        
        //2.设置约束
        
        //禁用autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        describeLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        
        //转轮
        let attribute = NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        addConstraint(attribute)
        
        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -44))
        
        //遮盖
        addConstraint(NSLayoutConstraint.init(item: shadowView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: shadowView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: shadowView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: shadowView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //小房子
        addConstraint(NSLayoutConstraint.init(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //描述文字
        addConstraint(NSLayoutConstraint.init(item: describeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: describeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
         addConstraint(NSLayoutConstraint.init(item: describeLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 255))
        
        //注册按钮
        addConstraint(NSLayoutConstraint.init(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: describeLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
         addConstraint(NSLayoutConstraint.init(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint.init(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        //登录按钮
        addConstraint(NSLayoutConstraint.init(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: describeLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint.init(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint.init(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        

    }
    
    //MARK 按钮点击事件的监听
    @objc private func registerBtnDidClick() {
    
        delegate?.visitorViewDidClickRegisterBtn()
    }
    @objc private func loginBtnDidClick() {
        
        delegate?.visitorViewDidClickLoginBtn()
    }
    
    //MARK 懒加载
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //小房子
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //添加遮盖
    private lazy var shadowView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    //文字描述
    private lazy var describeLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人！看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        //换行
        label.numberOfLines = 0
        //字体
        label.font = UIFont.systemFontOfSize(18)
        
        return label
        
    }()
    //注册按钮
    private lazy var registerBtn:UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加按钮点击事件
        btn.addTarget(self, action: Selector("registerBtnDidClick"), forControlEvents:UIControlEvents.TouchUpInside)

        return btn
    }()
    //登录按钮
    private lazy var loginBtn:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: Selector("loginBtnDidClick"), forControlEvents:UIControlEvents.TouchUpInside)
        return btn
        
    }()
    
}
